package com.openlogic.flow.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import com.openlogic.flow.common.enums.FlowCategoryStatus;
import com.openlogic.flow.domain.entity.FlowCategory;
import com.openlogic.flow.domain.entity.table.FlowCategoryTableDef;
import com.openlogic.flow.domain.entity.table.Tables;
import com.openlogic.flow.domain.query.FlowCategoryQuery;
import com.openlogic.flow.mapper.FlowCategoryMapper;
import com.openlogic.flow.service.IFlowCategoryService;

/**
 * @ClassName: OpenLogicFlowCategoryServiceImpl
 * @Description: 流程类型 服务实现层
 * @author weiyongpeng
 * @date 2025年2月11日 下午8:24:35
 */
@Component
public class FlowCategoryServiceImpl extends ServiceImpl<FlowCategoryMapper, FlowCategory> implements IFlowCategoryService {
	
	@Inject
	FlowCategoryMapper flowCategoryMapper;

	@Override
	public List<FlowCategory> selectTreeDataList(FlowCategoryQuery flowCategoryQuery) {
		FlowCategoryTableDef parent =  Tables.flowCategory.as("p");
		FlowCategoryTableDef childern =  Tables.flowCategory.as("c");
		QueryWrapper queryWrapper = new QueryWrapper()
		    .select(childern.id,childern.parentId,parent.name.as("parentName"),childern.type,childern.name,childern.status,childern.treeLevel)
		    .from(childern)
		    .leftJoin(parent).on(childern.parentId.eq(parent.id))
		    .where(childern.status.eq(FlowCategoryStatus.ABLE.getType()));
		// 类型
		if (StringUtils.isNotBlank(flowCategoryQuery.getCategoryTypeName())) {
			queryWrapper.and(childern.type.eq(flowCategoryQuery.getCategoryTypeName()));
		}
		// 名称
		if (StringUtils.isNotBlank(flowCategoryQuery.getCategoryName())) {
			queryWrapper.and(childern.name.eq(flowCategoryQuery.getCategoryName()));
		}
		return flowCategoryMapper.selectListByQuery(queryWrapper);
	}

	@Override
	public boolean update(FlowCategory flowCategory) {
		return flowCategoryMapper.update(flowCategory) > 0 ? Boolean.TRUE : Boolean.FALSE;
	}

	@Override
	public boolean insert(FlowCategory flowCategory) {
		return flowCategoryMapper.insert(flowCategory) > 0 ? Boolean.TRUE : Boolean.FALSE;
	}

	@Override
	public List<FlowCategory> selectByPid(String pid) {
		return flowCategoryMapper.selectListByPid(pid);
	}

	@Override
	public boolean delete(String id) {
		return flowCategoryMapper.deleteById(id) > 0 ? Boolean.TRUE : Boolean.FALSE;
	}

	@Override
	public FlowCategory info(Integer categoryId) {
        return flowCategoryMapper.selectOneById(categoryId);
	}

	@Override
	public List<FlowCategory> excludeChild(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

}
